﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式之禅.访问者模式
{
    /// <summary>
    /// 抽象角色类
    /// </summary>
    public abstract class AbstractElement
    {
        public string Version { get; set; }
        public abstract string Accept(IVisitor visitor);
    }

    /// <summary>
    /// 具体元素A
    /// </summary>
    public class ElementA : AbstractElement
    {
        public ElementA()
        {
            this.Version = " AAA ";
        }
        public override string Accept(IVisitor visitor)
        {
            return visitor.Visitor(this);
        }
    }

    /// <summary>
    /// 具体元素B
    /// </summary>
    public class ElementB : AbstractElement
    {
        public ElementB()
        {
            this.Version = " BBB ";
        }
        public override string Accept(IVisitor visitor)
        {
            return visitor.Visitor(this);
        }
    }

    /// <summary>
    /// 抽象访问者
    /// </summary>
    public interface IVisitor
    {
        string Visitor(ElementA a);
        string Visitor(ElementB b);
    }

    /// <summary>
    /// 具体访问者
    /// </summary>
    public class ConcreteVisitor : IVisitor
    {
        public string Visitor(ElementA a)
        {
            return "Visitor A " + a.Version;
        }

        public string Visitor(ElementB b)
        {
            return "Visitor B " + b.Version;
        }
    }

    public class ObjectStructure
    {
        private List<AbstractElement> elements = new List<AbstractElement>();
        public List<AbstractElement> Elements { get { return elements; } }

        public ObjectStructure()
        {
            elements.Add(new ElementA());
            elements.Add(new ElementA());
            elements.Add(new ElementB());
            elements.Add(new ElementB());
        }
    }


}
